package pa3.gui;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Timer;


import pa3.Host;
import pa3.HostImpl;
import pa3.HostImpl;
import pa3.HostState;

public class HostModel implements Observer{
	
	//private HostImpl host;
	private HostState hostState;
	private String hostName;
	private int failureProbability;
	private String log;
	private HashMap<String, Timer> checkTimers = new HashMap<String, Timer>();
	private HostController controller;
	private boolean isCrashed;
	private HostImpl host;
	
	public HostModel(HostController controller, String hostName, int failureProbability) {
		this.controller = controller;
		try {
			hostState = new HostState(hostName, failureProbability);
			hostState.addObserver(this);
			host = new HostImpl(hostState);
			new Thread(host).start();
		} catch (RemoteException e) {
			e.printStackTrace();
		}		
	}
	@Override
	public void update(Observable hostState, Object property) {		
//		synchronized(checkTimers){
//			if(((HostState) hostState).getCheckTimers() != null){
//				HashMap<String, Timer> timers = (HashMap<String, Timer>) ((HostState) hostState).getCheckTimers().clone();
//				Iterator<String> iter = timers.keySet().iterator();
//				while(iter.hasNext()){
//					String host = iter.next();
//					
//					//only add new timers
//					if(!checkTimers.containsKey(host)){
//						controller.addNewProgressbarFor(host);
//						Timer timer = new Timer(500, new CheckTimerListener(host));
//						checkTimers.put(host, timer);
//						timer.start();
//					}			
//				}
//				
//				//remove timers which are not in the hostState's map anymore
//				ArrayList<String> toRemove = new ArrayList<String>();
//				iter = checkTimers.keySet().iterator();
//				while(iter.hasNext()){
//					String host = iter.next();
//					
//					if(!timers.containsKey(host)){
//						toRemove.add(host);
////						controller.removeProgressbarFor(host);
////						checkTimers.remove(host);
//					}
//				}
//				
//				for(String removeHost : toRemove){
//					controller.removeProgressbarFor(removeHost);
//					checkTimers.remove(removeHost);
//				}
//			}
//		}
//		
		this.isCrashed = ((HostState) hostState).isCrashed();
		controller.setCrashed(this.isCrashed);
		
		this.log = ((HostState) hostState).getLogString();
		controller.setLogString(this.log);
	}
	public void crashHost() {
		this.host.crash();
	}
	
//	class CheckTimerListener implements ActionListener{
//		private int i = 0;
//		private String hostName;
//		
//		public CheckTimerListener(String hostName){
//			this.hostName = hostName;
//		}
//		@Override
//		public void actionPerformed(ActionEvent e) {
//			if(i >= Host.CHECK_WAIT_TIME){
//				//Toolkit.getDefaultToolkit().beep();
//				i = 0;
//				controller.removeProgressbarFor(hostName);
//			}
//			else{
//				i += 500;
//				controller.updateProgressbarFor(hostName, i);
//			}
//		}		
//	}


}
